﻿<modal id="copy-attribute-options-dialog" sm-render-at-page-end="true" sm-size="Medium">
    <modal-header sm-title="@T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.CopyOptions")"></modal-header>
    <modal-body>
        <form asp-action="CopyAttributeOptions" id="copy-options-form" method="POST">
            <input type="hidden" name="productVariantAttributeId" value="" />
            <input type="hidden" name="optionsSetId" value="" />

            <div id="copy-options-hint-template" class="hide">@T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.CopyOptionsHint")</div>
            <p id="copy-options-hint"></p>
            
            <div>
                <p>@T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.AskExistingValues")</p>
                <div class="form-check">
                    <input type="radio" class="form-check-input" name="deleteExistingValues" id="deleteExistingValues_1" value="false" checked="checked" />
                    <label class="form-check-label" for="deleteExistingValues_1">
                        @T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.MergeExistingValues")
                    </label>
                </div>
                <div class="form-check">
                    <input type="radio" class="form-check-input" name="deleteExistingValues" id="deleteExistingValues_2" value="true" />
                    <label class="form-check-label" for="deleteExistingValues_2">
                        @T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.DeleteExistingValues")
                    </label>
                </div>
            </div>
        </form>
    </modal-body>
    <modal-footer>
        <button class="btn btn-secondary btn-flat" data-dismiss="modal">
            <span>@T("Common.Cancel")</span>
        </button>
        <button id="btn-copy-attribute-options" class="btn btn-primary" data-dismiss="modal">
            <span>@T("Common.OK")</span>
        </button>
    </modal-footer>
</modal>

<script sm-target-zone="Scripts" data-origin="copy-attribute-options-dialog">
    $(function () {
        // Show modal dialog to copy attribute options.
        $(document).on('change', '#productvariantattributes-grid select[id^=OptionsSetsChoice]', function (e) {
            e.stopPropagation();

            var select = $(e.currentTarget),
                form = $('#copy-options-form'),
                valueCount = parseInt(select.attr('data-valcount')),
                productVariantAttributeId = select.attr('id').replace('OptionsSetsChoice', '');

            form.find('input[name=productVariantAttributeId]').val(productVariantAttributeId);
            form.find('input[name=optionsSetId]').val(select.val());

            if (valueCount > 0) {
                var hint = $('#copy-options-hint-template').text().replace('{0}', select.find(':selected').text());
                $('#copy-options-hint').text(hint);
                $('#copy-attribute-options-dialog').modal('show');
            }
            else {
                $('#btn-copy-attribute-options').trigger('click');
            }
        });

        // Copy attribute options.
        $(document).on('click', '#btn-copy-attribute-options', (function () {
            $('#copy-options-form').ajax({
                success: function () {
                    window["productvariantattributes-grid"].$children[0].read();
                }
            });
        }));
    });
</script>